import java.util.ArrayList;
import java.util.List;

/**
 * 1897. 重新分配字符使所有字符串都相等
 * https://leetcode-cn.com/problems/redistribute-characters-to-make-all-strings-equal/
 */
public class Solutions_1897 {
    public static void main(String[] args) {
        String[] words1 = {"abc", "aabc", "bc"};  // output: true
        String[] words2 = {"ab", "a"};  // output: false
        String[] words3 = {"abbab"};  // output: true
        String[] words4 = {"caaaaa", "aaaaaaaaa", "a", "bbb", "bbbbbbbbb", "bbb", "cc", "cccccccccccc", "ccccccc", "ccccccc", "cc", "cccc", "c", "cccccccc", "c"};  // output: true

        List<String[]> inputs = new ArrayList<>();
        inputs.add(words1);
        inputs.add(words2);
        inputs.add(words3);
        inputs.add(words4);

        for (String[] input : inputs) {
            boolean result = makeEqual(input);
            System.out.println(result);
        }
    }

    public static boolean makeEqual(String[] words) {
        if (words.length <= 1) {
            return true;
        }
        int[] map = new int[26];
        // 统计各字符出现的次数
        for (String word : words) {
            char[] arr = word.toCharArray();
            for (char c : arr) {
                map[c - 'a'] ++;
            }
        }
        int len = words.length;
        // 各字符的次数必须整除 len，否则无法使所有字符串都相等
        for (int i = 0; i < 26; i++) {
            if (map[i] % len != 0) {
                return false;
            }
        }
        return true;
    }
}
